<!--  线 + 柱混合图 -->
<template>
  <div ref="echartRef" :class="className" :style="{ height, width }"></div>
</template>

<script setup lang="ts">
import * as echarts from "echarts";
import { onMounted, ref, markRaw, watch, reactive } from "vue";
const echartRef = ref<HTMLElement | null>(null);

const props = defineProps({
  className: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "100%",
    required: true,
  },
  height: {
    type: String,
    default: "100%",
    required: true,
  },
  datas: {
    type: Object,
    default: () => {
      return {
        name: "",
        unit: "",
        data: [],
      };
    },
    required: true,
  },
});

function getOption() {
  var a = 0;
  props.datas.data.map((item) => {
    a += Number(item.value);
  });
  let option = {
    color: ["#E6C65B", "#796EDB", "#55E6C1"],
    tooltip: {
      trigger: "item",
    },
    title: {
      zlevel: 0,
      text: [
        '{value|' + a + '亩' + '}',
        '{name|总数}'
      ].join('\n'),
      top: 'center',
      left: '48%',
      textAlign: 'center',
      textStyle: {
        rich: {
          value: {
            color: '#FFFFFF',
            fontSize: 21,
            fontWeight: 'bold',
            lineHeight: 30
          },
          name: {
            color: '#FFFFFF',
            fontSize: 16,
            lineHeight: 25
          }
        }
      }
    },
    series: [
      {
        name: props.datas.name,
        type: "pie",
        radius: ["45%", "80%"],
        label: {
          show: true,
          color: "#FFFFFF",
        },
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)",
          },
        },
        data: props.datas?.data,
      },
    ],
  };
  return reactive(option);
}
let chart = ref<any>("");
onMounted(() => {
  // 图表初始化
  chart.value = markRaw(echarts.init(echartRef.value));

  chart.value.setOption(getOption());

  // 大小自适应
  window.addEventListener("resize", () => {
    chart.value.resize();
  });
});
watch(
  props,
  (data) => {
    chart.value.setOption(getOption());
  },
  { deep: true }
);
</script>
<style lang="scss" scoped>
.title {
  display: flex;
  justify-content: space-between;

  .download {
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}
</style>
